package de.slothsoft.commons.swing.treetable;

import java.awt.Component;
import java.awt.Graphics;

import javax.swing.JTable;
import javax.swing.JTree;
import javax.swing.table.TableCellRenderer;
import javax.swing.tree.TreeModel;

public class DefaultTreeTableCellRenderer extends JTree implements TableCellRenderer {

	private static final long serialVersionUID = 1L;

	private int visibleRow;
	private JTreeTable treeTable;

	public DefaultTreeTableCellRenderer(TreeModel model) {
		super(model);
	}

	@Override
	public void setBounds(int x, int y, int w, int h) {
		int height = this.treeTable == null ? 0 : this.treeTable.getHeight();
		super.setBounds(x, 0, w, height);
	}

	@Override
	public void paint(Graphics g) {
		int rowHeight = this.treeTable == null ? 0 : getRowHeight();
		g.translate(0, -this.visibleRow * rowHeight);
		super.paint(g);
	}

	@Override
	public Component getTableCellRendererComponent(JTable table, Object value,
			boolean isSelected, boolean hasFocus, int row, int column) {
		if (isSelected) {
			setBackground(table.getSelectionBackground());
		} else {
			setBackground(table.getBackground());
		}
		this.visibleRow = row;
		this.treeTable = (JTreeTable) table;
		return getTreeTableCellRendererComponent(this.treeTable, value,
				isSelected, hasFocus, row, column);
	}

	public Component getTreeTableCellRendererComponent(JTreeTable treeTable,
			Object value, boolean isSelected, boolean hasFocus, int row,
			int column) {
		return this;
	}
}
